<template>
  <!-- 上传配件索赔申请  -->
  <div>
    <FilterTable
      ref="table"
      columnsRef="waxUploadClaimAb"
      :columns="columns"
      :onSyncTableData="getTableData"
      :params="fetchParams"
      :dataSource="tempData"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="selectRows"
      :isMemoryPagination="true"
    >
      <template slot="moreActions">
        <!-- <span @click="() => removeRecordHandle('Wts')">删除数据</span> -->
      </template>
      <!-- :fetchapi="fetchApi" -->
      <template slot="controls" slot-scope="props">
        <UploadFile
          style="margin: 0 10px;"
          :initial-value="fileInitalValue"
          isOnlyButton
          :limit="20"
          :disabled="addBtnDisabled"
          type="primary"
          icon="el-icon-download"
          :actionUrl="`${server.DMSCLOUD_PART}/dealer/parts/claim/import`"
          @change="handleUploadChange"
          >批量导入</UploadFile
        >
        <AjaxButton size="small" type="primary" :click="exportModel">导入模板下载</AjaxButton>
        <!-- <el-button size="small" type="primary" icon="el-icon-plus" @click="batchImport">批量导入</el-button> -->
        <el-button size="small" type="primary" icon="el-icon-plus" @click="uploadHandle">数据上传</el-button>
        <el-button size="small" type="primary" icon="el-icon-plus" @click="addHandle">新建</el-button>
        <AjaxButton :click="deleteHandle" type="primary">删除</AjaxButton>
        <!-- <el-button size="small" type="primary" icon="el-icon-plus" @click="exportHandle">导出</el-button>
        <AjaxButton :click="printHandle" type="primary">打印</AjaxButton> -->
      </template>
    </FilterTable>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { uploadPartsClaim, exportClaimModel } from '@/api/part/report/uploadClaim';
import { claimType, applicationType, yesOrNo, reasonCode } from './util';
import { isAuthorized, arrayObjDeWeight, notifyAction, exportExcelFile } from '@/utils/index';
import SERVER from '@/api/server';
export default {
  name: 'UploadClaim',
  components: {},
  mixins: [dictionary],
  props: [],
  data() {
    return {
      columns: this.createUploadClaimColumns(),
      fetchParams: { xhrAbort: true },
      tempData: [],
      tableData: [],
      // 选中行数据
      selectRowsData: [],
      fileInitalValue: [], // 上传文件
      addBtnDisabled: false,
      server: SERVER
    };
  },
  methods: {
    // 导出模板
    exportModel() {
      exportClaimModel().then(res => {
        exportExcelFile(res, '上传配件索赔申请模板');
      });
    },
    // 向table列表中插入数据
    handleDetailSelectConfirm(e, data) {
      let partList = [];

      e.forEach(element => {
        let temp = {};
        // element.map(x => console.log(x));
        for (const key in element) {
          // 将值翻译成code
          let dataKey = key.toUpperCase();
          let value = element[key];
          temp[dataKey] = value;
        }

        partList.push(temp);
      });
      console.log('ceshi ', partList);
      this.$refs.table.EXECUTE_INSERT(partList);
      this.$notify.success({ title: '提示信息', message: '导入成功' });
    },
    // 导入成功事件
    handleUploadChange(e) {
      console.log('导入文件上传成功', e[0].url);
      this.fileInitalValue = e;
      let excelData = e[0].url.result;
      console.log(excelData, 'excelData');
      console.log(this.fileInitalValue, 'excelDatssssa');
      if (excelData.length) {
        // this.formatPartsData(excelData);
        this.handleDetailSelectConfirm(excelData, e[0].url.errorList);
      }
    },
    selectRows(val) {
      this.selectRowsData = [];
      this.selectRowsData = val;
    },
    // 移除当前行
    removeRecordHandle() {},
    // 异步获取表单数据
    async getTableData(val) {
      this.tableData = val;
    },
    // 打印
    printHandle() {},
    // 导出
    exportHandle() {},
    // 索赔代码申请
    searchClaimCodeHandle() {},
    // 数据上传
    async uploadHandle() {
      if (this.tableData.length < 1) {
        return this.$notify.error({ title: '提示信息', message: '请选中一条数据' });
      }
      // 表单校验
      let listErr = this.$refs.table.GET_REQUIRED_ERROR();
      if (listErr !== '' && listErr !== undefined && listErr !== null) {
        return this.$notify.error({ title: '提示信息', message: '请填写表单' });
      }
      let params = Object.assign({ TableParameter: { IT_BJSP: this.tableData }, ImportParameter: {} });
      let res = await uploadPartsClaim(params);
      if (res.resultCode == 200) {
        return this.$notify({ title: '提示信息', message: res.data });
      }
      console.log('保存结果', res);
    },
    // 批量导入
    batchImport() {},
    // 删除处理
    deleteHandle(val) {
      if (this.selectRowsData.length == 0) {
        return this.$notify.error({ title: '提示信息', message: '请选中一条数据' });
      }
      this.$refs.table.EXECUTE_DELETE(this.selectRowsData);
    },
    // 新增处理
    addHandle() {
      this.$refs.table.EXECUTE_INSERT({ DVBELN: '', QS: 'N', SPLX: 'C', SQLX: 'BF', MATNR: '', SPSL: '' });
    },
    // 创建table字段
    createUploadClaimColumns() {
      return [
        {
          title: '发运单号',
          dataIndex: 'DVBELN',
          sorter: true,
          filter: true,
          filterType: 'input',
          editable: true,
          editRequired: true,
          editType: 'text'
        },
        {
          title: '配件代码',
          dataIndex: 'MATNR',
          sorter: true,
          filter: true,
          filterType: 'input',
          editable: true,
          editRequired: true,
          editType: 'text'
        },
        {
          title: '索赔申请数量',
          dataIndex: 'SPSL',
          min: 1,
          sorter: true,
          filter: true,
          filterType: 'number',
          editable: true,
          editType: 'number',
          editRequired: true
        },
        {
          title: '索赔类型',
          dataIndex: 'SPLX',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: claimType(),
          editable: true,
          editType: 'select',
          editItems: claimType(),
          editRequired: true
        },
        {
          title: '申请类型',
          dataIndex: 'SQLX',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: applicationType(),
          editable: true,
          editType: 'select',
          editItems: applicationType(),
          editRequired: true
        },
        {
          title: '是否提出问题',
          dataIndex: 'QS',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: yesOrNo(),
          editable: true,
          editType: 'checkbox',
          editItems: yesOrNo()
        },
        {
          title: '索赔原因代码',
          dataIndex: 'JTQK',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          editable: true,
          editRequired: true,
          editType: 'select',
          editItems: reasonCode()
        },
        // {
        //   title: '索赔原因名称',
        //   dataIndex: 'JTQKNAME',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input',
        //   editable: true
        // },
        {
          title: '拒绝原因',
          dataIndex: 'JTQK_REASON',
          sorter: true,
          filter: true,
          filterType: 'input',
          editable: true,
          editType: 'text'
        }
      ];
    }
  }
};
</script>

<style></style>
